﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Movement_Trajectory_Analysis
{
    public partial class AnalyzeForm : Form
    {
        MainForm mainForm;
        //MTA mta;
        MTAF mtaf;

        public AnalyzeForm(MainForm mainForm)
        {
            this.mainForm = mainForm;
            InitializeComponent();
            mtaf = new MTAF();
        }

        private void AnalyzeForm_Resize(object sender, EventArgs e)
        {
            pictureAnalize.Width = (ActiveForm.Width / 2) - 20;
            analyzeResault.Width = (ActiveForm.Width / 2) - 20;
            panel1.Height = ActiveForm.Height / 10;
        }

        private void AnalyzeForm_Load(object sender, EventArgs e)
        {
            try
            {
                mainForm.mta.MTA_open(mainForm.OpenPicture);

                //pictureAnalize.Width = 300;
                //analyzeResault.Width = 300;

                Bitmap tmp = new Bitmap(mainForm.OpenPicture.FileNames[0]);
                //analyzeResault.Width = tmp.Width;
                //analyzeResault.Height = tmp.Height;

                Size size = new Size(tmp.Width / 2, tmp.Height / 2);
                pictureAnalize.Image = new Bitmap(tmp, size);

                mainForm.mta.get_color(mainForm.ColorList);
                progressAnalyzeBar.Maximum = mainForm.OpenPicture.FileNames.Length - 1;
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }

        private void analysisWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            mainForm.mta.move_analisis_opt_extream_Threading();
            //analysisWorker.ReportProgress(mta.loading);
            //mta.draw(analyzeResault);
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            analysisWorker.RunWorkerAsync();
            //mta.move_analysis_opt();
            //mta.draw(analyzeResault);

        }

        private void analysisWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            //labelProgressAnalysis.Text = e.ProgressPercentage.ToString();
            progressAnalyzeBar.Value = e.ProgressPercentage;
            pictureAnalize.Image = new Bitmap(mainForm.OpenPicture.FileNames[e.ProgressPercentage]);
        }
        //-----------------------------------------------------------------------------------------------------------------------


        private void buttonDraw_Click(object sender, EventArgs e)
        {
            //textBox1.Text = Convert.ToString(MTA.liczbaP);
            mainForm.mta.draw(analyzeResault);

            //sprawdzenie checkbox'a
            //    if ( checkBox1.Checked == true )
            //    MTAF.Zapisz("mtaproject");
            //
        }

        private void analysisWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            progressAnalyzeBar.Value = 0;
            MessageBox.Show("Analysis completed !", "Alert");

            buttonDraw_Click(sender, e);
            buttonSaveGraph.Enabled = true;
            buttonSaveResults.Enabled = true;

        }

        private void buttonSaveGraph_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveGraph = new SaveFileDialog();
            saveGraph.Filter = "PNG files (*.png)|*.png|All files (*.*)|*.*";

            if (saveGraph.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    analyzeResault.Image.Save(saveGraph.FileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not save file on disk. Original error: " + ex.Message);
                }
            }
        }

        private void buttonSaveResults_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveResults = new SaveFileDialog();
            saveResults.Filter = "mta files (*.mta)|*.mta";

            if (saveResults.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    MTAF.SaveMTA(@saveResults.FileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not save file on disk. Original error: " + ex.Message);
                }
            }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void check_Click(object sender, EventArgs e)
        {
            CheckForm check = new CheckForm(mainForm);
            check.Show();
            //CheckForm.new
            // MLP.Neuron
           // MTA.trajectory.GetLength
        }
    }
}
